package com.itheima.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.UmsMenuApi;
import com.itheima.api.UmsResourceApi;
import com.itheima.api.UmsRoleApi;
import com.itheima.api.UmsRoleMenuRelationApi;
import com.itheima.api.UmsRoleResourceRelationApi;
import com.itheima.pojo.UmsMenu;
import com.itheima.pojo.UmsResource;
import com.itheima.pojo.UmsRole;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author 袁鹏
 * @date 2022-10-13-10:12
 */
@Service
public class UmsRoleService {

    @DubboReference
    private UmsRoleMenuRelationApi umsRoleMenuRelationApi;

    @DubboReference
    private UmsRoleResourceRelationApi umsRoleResourceRelationApi;

    @DubboReference
    private UmsRoleApi umsRoleApi;

    @DubboReference
    private UmsMenuApi umsMenuApi;

    @DubboReference
    private UmsResourceApi umsResourceApi;

    /**
     * 给角色分配菜单
     */
    public void allocMenu(Integer[] menuIds, Integer roleId) {
        if(menuIds.length > 0 && roleId != null) {
            umsRoleMenuRelationApi.allocMenu(menuIds, roleId);
        }
    }


    /**
     * 给角色分配资源
     */
    public void allocResource(Integer[] resourceIds, Integer roleId) {
        if(resourceIds.length > 0 && roleId != null){
            umsRoleResourceRelationApi.allocResource(resourceIds, roleId);
        }
    }


    /**
     * 添加新角色
     */
    public void createRole(UmsRole role) {
        umsRoleApi.createRole(role);
    }


    /**
     * 批量删除角色
     */
    public void deleteMulti(Integer[] ids) {
        if(ids.length > 0) {
            // 删除角色
            umsRoleApi.deleteMulti(ids);
            // 删除角色与目录的关联
            umsRoleMenuRelationApi.deleteByRoleId(ids);
            // 删除角色与资源的关联
            umsRoleResourceRelationApi.deleteByRoleId(ids);
        }
    }


    /**
     * 根据角色名称获取角色列表
     */
    public PageResult pageRoleByName(String keyword, Integer pageNum, Integer pageSize) {
        Page<UmsRole> pageInfo = umsRoleApi.pageRoleByName(keyword, pageNum, pageSize);
        return new PageResult<>(Convert.toInt(pageInfo.getCurrent()), Convert.toInt(pageInfo.getSize()), Convert.toInt(pageInfo.getTotal()), Convert.toInt(pageInfo.getCurrent()), pageInfo.getRecords());
    }

    public List<UmsRole> list() {
        return umsRoleApi.list();
    }


    /**
     * 获取角色相关菜单
     */
    public List<UmsMenu> getRelatedMenu(Integer roleId) {
        List<Long> menuIdList = umsRoleMenuRelationApi.getRoleRelatedMenuId(roleId);
        return umsMenuApi.findMenus(menuIdList);
    }


    /**
     * 获取角色相关资源
     */
    public List<UmsResource> getRelatedResource(Integer roleId) {
        List<Long> resourceIdList = umsRoleResourceRelationApi.getRoleRelatedResourceId(roleId);
        return umsResourceApi.getById(resourceIdList);
    }


    /**
     * 修改角色
     */
    public void updateById(Integer roleId, UmsRole umsRole) {
        umsRole.setId(Convert.toLong(roleId));
        umsRoleApi.update(umsRole);
    }


    /**
     * 修改角色状态
     */
    public void updateStatus(Integer roleId, Integer status) {
        UmsRole umsRole = new UmsRole();
        umsRole.setId(Convert.toLong(roleId));
        umsRole.setStatus(status);
        umsRoleApi.update(umsRole);
    }
}
